﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Weazel.Willows.PreliminaryModel
{
	public class IdentifierMap
	{
		private Module module;

		private Dictionary<string, object> map;

		public void Add(string identifier, object obj)
		{
			if (map.ContainsKey(identifier))
			{
				string message = 
					string.Format("Duplicate definition of identifier '{0}' in module '{1}'", identifier, module.Name);

				throw new Exception(message);
			}
			
			map.Add(identifier, obj);
		}

		public void Replace(string identifier, object entity)
		{
			System.Diagnostics.Trace.Assert(map.ContainsKey(identifier));

			map[identifier] = entity;
		}

		public object Lookup(string identifier)
		{
			if (!map.ContainsKey(identifier))
			{
				string message =
					string.Format("No object associated with identifier '{0}' in module '{1}'", identifier, module.Name);

				Console.WriteLine("identifiers: (count: " + map.Count + ")");
				foreach (string key in map.Keys)
					Console.WriteLine("\t" + key);

				throw new KeyNotFoundException(message);
			}

			return map[identifier];
		}

		public IdentifierMap Clone(Module newModule)
		{
			IdentifierMap clone = new IdentifierMap(newModule);
			clone.map = this.map;
			return clone;
		}

		public IdentifierMap(Module module)
		{
			this.module = module;
			this.map = new Dictionary<string, object>();
		}
	}
}
